package com.aaa.outbound.controller;




import com.aaa.entity.outbound.*;
import com.aaa.entity.stock.Stock;
import com.aaa.outbound.feign.StockFeign;
import com.aaa.outbound.service.OutorderService;
import com.aaa.outbound.service.PicktaskDetailService;
import com.aaa.outbound.service.PicktaskOutorderService;
import com.aaa.outbound.service.PicktaskService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjf
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/outbound/picktask")
public class PicktaskController {
    @Autowired
    private PicktaskService picktaskService;
    @Autowired
    private OutorderService outorderService;
    @Autowired
    private PicktaskOutorderService picktaskOutorderService;
    @Autowired
    private StockFeign stockFeign;
    @Autowired
    private PicktaskDetailService picktaskDetailService;

    @PostMapping("searchPicktask/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询")
    public R searchPicktask(@ApiParam(value = "当前页数") @PathVariable Integer page,
                        @ApiParam(value = "每页个数") @PathVariable Integer limit,
                        @ApiParam(value = "查询条件") @RequestBody Picktask picktask){
        Page<Picktask> pageinfo=picktaskService.searchPicktask(page,limit,picktask);
        if (pageinfo==null){
            return R.error().message("没有查到数据");
        }
        return R.ok().message("分页查询成功").code(200).data("picktask",pageinfo);
    }

    @PostMapping("viewPicktask/{picktaskno}")
    @ApiOperation(value = "查看拣货任务明细")
    public R viewPicktask(@ApiParam(value = "拣货任务单号") @PathVariable("picktaskno") String picktaskno){
        List<ViewPicktask> viewPicktasks = picktaskService.selectViewPicktaskByPicktaskno(picktaskno);
        return R.ok().data("viewPicktask",viewPicktasks).code(200);
    }

    @PostMapping("returnPicktask/{picktaskno}")
    @ApiOperation(value = "退还拣货任务")
    public R returnPicktask(@ApiParam(value = "拣货任务单号") @PathVariable("picktaskno") String picktaskno){
        List<ViewPicktask> viewPicktasks = picktaskService.selectViewPicktaskByPicktaskno(picktaskno);
        String outorderno = viewPicktasks.get(0).getOutorderno();
        QueryWrapper<Outorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("outorderno", outorderno);
        Outorder outorder = outorderService.getOne(queryWrapper);
        String state = outorder.getState();
        if(state.equals("3")) {
            for(ViewPicktask viewPicktask:viewPicktasks) {
                String commoditybar = viewPicktask.getCommoditybar();
                String locatorcode = viewPicktask.getLocatorcode();
                Integer qty = viewPicktask.getQty();
                Stock stock = stockFeign.selectStockByComBarOriLo(commoditybar, locatorcode, 41L);
                stock.setLqty(stock.getLqty() - qty);
                stock.setUqty(stock.getUqty() + qty);
                stockFeign.updateById(stock);
                outorder.setState("2");
                outorderService.updateById(outorder);
                QueryWrapper<Picktask> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("picktaskno", picktaskno);
                picktaskService.remove(queryWrapper1);
                Long pid = outorder.getId();
                QueryWrapper<PicktaskDetail> queryWrapper2=new QueryWrapper<>();
                queryWrapper2.eq("pid",pid);
                picktaskDetailService.remove(queryWrapper2);
            }
            return R.ok().code(200).message("退还成功");
        }else {
            return R.error().message("退还失败，该任务未分配或已拣货");
        }
    }
}

